﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.Threading;
using Microsoft.TeamFoundation.Client;
using Microsoft.TeamFoundation.VersionControl.Client;
using TfsBugger.Core;

namespace TfsBugger.Buggers
{
    public class CheckInBugger : Bugger
    {
        public CheckInBugger() 
            : base()
        {

        }


        private List<Changeset> m_newChangeSets = new List<Changeset>();
        private VersionControlServer m_vcs;
        private TeamProject m_proj;
        private int m_lastChangeSetId;


        #region Implemented Abstract Methods
        public override bool CheckArgsLoaded()
        {
            return (null != Args && Args is CheckInBuggerArgs);
        }


        protected override void Init()
        {
            m_newChangeSets.Clear();
            CheckInBuggerArgs cargs = (CheckInBuggerArgs)Args;

            m_vcs = (VersionControlServer)cargs.ServerManager.Server.GetService(typeof(VersionControlServer));
            m_proj = m_vcs.GetTeamProject(cargs.ProjectName);
            if (null == m_proj)
                throw new TeamProjectNotFoundException(string.Format("Could not locate the '{0}' project.", cargs.ProjectName));

            m_lastChangeSetId = m_vcs.GetLatestChangesetId();
        }


        protected override void Poll()
        {
            int latest = m_vcs.GetLatestChangesetId();
            if (latest != m_lastChangeSetId)
            {
                m_lastChangeSetId = latest;
                Changeset latestChg = GetLatestChangeForProj(m_vcs, m_proj.ServerItem);
                if (null != latestChg && latestChg.ChangesetId == latest)
                {
                    m_newChangeSets.Add(latestChg);
                    RaiseTargetUpdated(
                        new TargetUpdatedEventArgs {
                            State = new CheckInBuggerState {
                                LatestChangeset = latestChg
                            }
                        });
                }
            }
        }
        #endregion


        #region Private Helpers
        private Changeset GetLatestChangeForProj(VersionControlServer vcs, string projPath)
        {
            Changeset latest = null;
            IEnumerable results = vcs.QueryHistory(
                projPath,
                VersionSpec.Latest,
                0,
                RecursionType.Full,
                null,
                null,
                null,
                1,
                true,
                true);

            foreach (object obj in results)
            {
                Changeset set = obj as Changeset;
                if (null == set)
                    continue;

                latest = set;                    
            }

            return latest;
        } 
        #endregion
    }
}
